﻿/*-----------------------------------------------
// File: LoginCheckVersion.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Login
{
    public class LoginCheckVersion : Fsm.FsmStateMonoBase<LoginState>
    {
        //string foreUpdateAddress;
        //string generalUpdateAddress;
        //string resUpdateAddress;
        LoginManager manager
        {
            get { return this.controller as LoginManager; }
        }
        public LoginCheckVersion(LoginState state, LoginManager ctrl) : base(state, ctrl) { }

        public override void Enter()
        {
            CheckVersion();
        }

        public override void Leave()
        {
            base.Leave();
        }

        void CheckVersion()
        {

            CSCheckVersion msg = new CSCheckVersion();
            msg.appVersion = GameConfig.Instance.AppVersion;
            msg.resVersoin = GameConfig.Instance.ResVersion;
            msg.channelId = GameConfig.Instance.GetChannelId;
            msg.channelName = GameConfig.Instance.GetChannelName;
            msg.deviceId = GameConfig.Instance.GetDeviceID;
            msg.deviceDesc = SystemInfo.deviceModel;
            AssetUpdateManager.Instance.CheckDownloadFull();
            msg.clientFull = GameConfig.Instance.IsDownloadOver ? 1 : 0;

            NetAPI.SendHttp(OpDefine.CSCheckVersion, msg, CheckVersionCallBack);
            Debug.Log("checkVersion send end");
        }
        /// <summary>
        /// 强更
        /// </summary>
        //void ForceUpdate()
        //{
        //    Application.OpenURL(foreUpdateAddress);
        //    Application.Quit();
        //}
        /// <summary>
        /// 非强更
        /// </summary>
        //void GeneralUpdate(bool b)
        //{
        //    Application.OpenURL(generalUpdateAddress);
        //    Application.Quit();
        //}
        /// <summary>
        /// 资源，数据更新
        /// </summary>
        //void ResUpdate()
        //{
        //    Application.OpenURL(resUpdateAddress);
        //    Application.Quit();
        //}
        void CheckVersionCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCCheckVersion msg = packet.kBody as SCCheckVersion;
                if (msg != null)
                {
                    GameConfig.Instance.announceUrl = msg.actionParam2;
                    GameConfig.Instance.serverAppVersion = msg.serverAppVersion;
                    GameConfig.Instance.serverResVersion = msg.serverResVersion;
                    GameConfig.Instance.resUrl = msg.resURL;
                    GameConfig.Instance.programMethod = msg.programMethod;
                    GameConfig.Instance.resMethod = (ResourceUpdateMethod)msg.resMethod;
                    GameConfig.Instance.appUrl = msg.appURL;

                    Debug.Log("update method " + msg.resMethod);


                    var dict = CommonMethod.ParseKeyValueEntry(msg.entry);

                    //是否是审核模式
                    if (dict.ContainsKey("isaudit"))
                    {
                        GameConfig.Instance.isaudit = (dict["isaudit"] == "1");
                    }
                    //是否需要激活
                    if (dict.ContainsKey("needActivation"))
                    {
                        GameConfig.Instance.needActivation = (dict["needActivation"] == "1");
                    }
                    //激活码获取地址
                    if (dict.ContainsKey("activationUrl"))
                    {
                        GameConfig.Instance.activationUrl = dict["activationUrl"];
                    }
                    //帧率
                    if (dict.ContainsKey("MaxFrameRateStr"))
                    {
                        var split = dict["MaxFrameRateStr"].Split(',');
                        int.TryParse(split[0], out RenderManager.highMaxFPS);
                        int.TryParse(split[1], out RenderManager.middleMaxFPS);
                        int.TryParse(split[2], out RenderManager.lowMaxFPS);
                    }
                    if (dict.ContainsKey("MaxRenderModel"))
                    {
                        var split = dict["MaxRenderModel"].Split(',');
                        int.TryParse(split[0], out RenderManager.highMaxModel);
                        int.TryParse(split[1], out RenderManager.middleMaxModel);
                        int.TryParse(split[2], out RenderManager.lowMaxModel);
                    }
                    //加载线程数
                    if (dict.ContainsKey("WWWCountStr"))
                    {
                        var split = dict["WWWCountStr"].Split(',');
                        int.TryParse(split[0], out RenderManager.highWWWCount);
                        int.TryParse(split[1], out RenderManager.middleWWWCount);
                        int.TryParse(split[2], out RenderManager.lowWWWCount);
                        switch (RenderManager.Quality)
                        {
                            case RenderLevel.High:
                                AssetLoad.AssetBundleManager.Instance.MAX_WWW_COUNT = RenderManager.highWWWCount;
                                break;
                            case RenderLevel.Middle:
                                AssetLoad.AssetBundleManager.Instance.MAX_WWW_COUNT = RenderManager.middleWWWCount;
                                break;
                            case RenderLevel.Low:
                                AssetLoad.AssetBundleManager.Instance.MAX_WWW_COUNT = RenderManager.lowWWWCount;
                                break;
                        }
                    }
                    //############测试接口
                    //GameConfig.Instance.serverAppVersion = "0.9.0.7";
                    //GameConfig.Instance.PreAppVersion = "0.9.0.1";
                    //GameConfig.Instance.serverResVersion = "00002";
                    //GameConfig.Instance.resUrl = "http://192.168.1.165/android";
                    //GameConfig.Instance.programMethod = 0;
                    //GameConfig.Instance.resMethod = ResourceUpdateMethod.SLIENCE_UPDATE;
                    //GameConfig.Instance.appUrl = "";
                    //##########

                    if (GameConfig.Instance.needActivation)
                    {
                        this.controller.SwitchState(LoginState.Activation);
                    }
                    else
                    {
                        this.controller.SwitchState(LoginState.SyncDataAndRes);
                    }

                }

                else
                {
                    Debug.LogError("Convert SCCheckVersion Falied.");
                }

            }
            else
            {
                Debug.LogError("Receive SCCheckVersion Failed.");
            }
        }


        /// <summary>
        /// 直接显示服务器面板
        /// </summary>
        void ShowServerPanel()
        {
            if (StaticData.UserName != "" && StaticData.PassWord != "")//缓存中已经输入过用户名密码了，不显示登录界面，直接到服务器列表界面
            {
                Debug.Log("缓存中已经输入过用户名密码了，不显示登录界面，直接到服务器列表界面");

                manager.SwitchState(LoginState.GetServerList);

            }
        }

    }

}
